﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;

using System.Reflection;
using System.IO;

using Sherwood.SignOn.Server.Models;

namespace Sherwood.SignOn.Server.Models.Services
{
    /// <summary>
    /// Provides access to resources bound to a specific Http / execution context.
    /// For example, a translation provider or other types of content filters.
    /// </summary>
    public class Context
    {
        private static string _executionDirectory = null;

        [ThreadStatic]
        private static Dictionary<string, object> _threadContextItems;

        /// <summary>
        /// Returns a dictionary of context items for the current execution context.
        /// If a HttpContext exists, the HttpContext.Current.Items property will be returned.
        /// Otherwise, a thread-specific dictionary will be returned.
        /// </summary>
        public static IDictionary ContextItems
        {
            get
            {
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Items != null)
                {
                    return System.Web.HttpContext.Current.Items;
                }
                else
                {
                    if (_threadContextItems == null)
                    {
                        _threadContextItems = new Dictionary<string, object>();
                    }
                    return _threadContextItems;
                }

            }
        }

      
      /*  /// <summary>
        /// Current translation provider for the application.
        /// A translation provider can be used to provide translations for static "language" strings.
        /// </summary>
        public static ITranslationProvider Translator
        {
            get
            {
                ITranslationProvider translationProvider = null;
                if (ContextItems.Contains("TranslationProvider"))
                {
                    translationProvider = ContextItems["TranslationProvider"] as ITranslationProvider;
                }
                if (translationProvider == null)
                {
                    translationProvider = Sherwood.DependencyInjection.Resolve<ITranslationProvider>();
                    ContextItems["TranslationProvider"] = translationProvider;
                }
                return translationProvider;
            }
            set
            {
                ContextItems["TranslationProvider"] = value;
            }
        }*/

        /// <summary>
        /// Retreives the path to the execution directory (Bin folder) of the application.
        /// </summary>
        public static string BinDirectory
        {
            get
            {
                if (_executionDirectory == null)
                {
                    if (HttpContext.Current != null && HttpContext.Current.Server != null)
                    {
                        _executionDirectory = HttpContext.Current.Server.MapPath("~/bin");
                    }
                    else
                    {
                        //Get location of main assembly (including filename)
                        string location = Assembly.GetAssembly(typeof(Context)).Location;

                        FileInfo file = new FileInfo(location);

                        //Retreive just filename
                        _executionDirectory = file.DirectoryName;
                    }
                }
                return _executionDirectory;
            }
        }

        /// <summary>
        /// Retreives the path to the resources directory (../Bin/Resources) of the application.
        /// </summary>
        public static string ResourcesDirectory
        {
            get
            {
                return BinDirectory + "\\Resources";
            }
        }

        /// <summary>
        /// Retreives the path to the resources directory (../Bin/Resources) of the application.
        /// </summary>
        public static string ConfigurationDirectory
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Server != null)
                {
                    return HttpContext.Current.Server.MapPath("~/Config");
                }
                else
                {
                    return BinDirectory + "\\Config";
                }
            }
        }

        /// <summary>
        /// Returns true if the current client device is an iPhone
        /// </summary>
        public static bool IsIPhone
        {
            get
            {
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null && System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"] != null)
                    return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains("iphone");
                return false;
            }
        }

        /// <summary>
        /// Returns true if client device is a mobile device.
        /// </summary>
        // Source: http://www.codeproject.com/KB/aspnet/mobiledetect.aspx
        public static bool IsMobileDevice
        {
            get
            {

                //GETS THE CURRENT USER CONTEXT
                HttpContext context = System.Web.HttpContext.Current;

                if (context.Request != null)
                {

                    //FIRST TRY BUILT IN ASP.NT CHECK
                    if (context.Request.Browser.IsMobileDevice)
                    {
                        return true;
                    }
                    //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
                    if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
                    {
                        return true;
                    }
                    //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
                    if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
                        context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
                    {
                        return true;
                    }
                    //AND FINALLY CHECK THE HTTP_USER_AGENT 
                    //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
                    if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
                    {
                        //Create a list of all mobile types
                        string[] mobiles =
                            new[]
                {
                    "midp", "j2me", "avant", "docomo", 
                    "novarra", "palmos", "palmsource", 
                    "240x320", "opwv", "chtml",
                    "pda", "windows ce", "mmp/", 
                    "blackberry", "mib/", "symbian", 
                    "wireless", "nokia", "hand", "mobi",
                    "phone", "cdm", "up.b", "audio", 
                    "SIE-", "SEC-", "samsung", "HTC", 
                    "mot-", "mitsu", "sagem", "sony"
                    , "alcatel", "lg", "eric", "vx", 
                    "NEC", "philips", "mmm", "xx", 
                    "panasonic", "sharp", "wap", "sch",
                    "rover", "pocket", "benq", "java", 
                    "pt", "pg", "vox", "amoi", 
                    "bird", "compal", "kg", "voda",
                    "sany", "kdd", "dbt", "sendo", 
                    "sgh", "gradi", "jb", "dddi", 
                    "moto", "iphone"
                };

                        //Loop through each item in the list created above 
                        //and check if the header contains that text
                        foreach (string s in mobiles)
                        {
                            if (context.Request.ServerVariables["HTTP_USER_AGENT"].
                                                                ToLower().Contains(s.ToLower()))
                            {
                                return true;
                            }
                        }
                    }                    
                }
                return false;
            }
        }

    }
}
